//
//  UIControl+TapAction.m
//  魔兽大数据
//
//  Created by 王右 on 2017/12/8.
//  Copyright © 2017年 王右. All rights reserved.
//

#import "UIControl+TapAction.h"
#import <objc/runtime.h>

static char kActionHandlerBlockKey;


@implementation UIControl (TapAction)


- (void)setActionWithBlock:(void (^)(UIControl *))block{
    objc_setAssociatedObject(self, &kActionHandlerBlockKey, block, OBJC_ASSOCIATION_COPY);
    [self addTarget:self action:@selector(handleAction) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)handleAction{
    void(^action)(UIControl *control) = objc_getAssociatedObject(self, &kActionHandlerBlockKey);
    if (action) {
        action(self);
    }
}

@end
